<?php

namespace App\Models\Cargo;

use App\Models\Comment\Comments;
use App\Models\User\User;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class WayBill extends Model
{
    protected $table = 'waybills';

    public $dateFormat = 'Y-m-d H:i:s';
//状态：0为已取消、1为待付款、2为运输中、3为待评价，4已完成，5已投诉，6已回复
    private $status_text = ['已取消', '待付款', '运输中', '待评价', '已完成', '已投诉', '已回复'];
    private $entrust_status_text = ['否', '是'];
    private $dstatus_text = ['未装车', '已装车'];
    private $ostatus_text = ['未装车', '已装车'];
    private $owner_pay_status_text = ['未支付', '已支付','已退回余额'];
    private $driver_pay_status_text = ['未支付', '已支付','已退回余额'];
    private $owner_arrive_text = ['未到达', '已到达'];
    private $driver_arrive_text = ['未到达', '已到达'];
    private $is_upload_text = ['未上传', '已上传'];
    private $is_unload_text = ['未卸货', '已卸货'];
    private $is_owner_comment_text = ['未评价', '已评价'];
    private $is_driver_comment_text = ['未评价', '已评价'];
    protected $appends = ['friendly_time'];

    //运单状态
    public function getStatusTextAttribute()
    {
        return $this->status_text[$this->attributes['status']];
    }
    //是否委托单
    public function getEntrustStatusTextAttribute()
    {
        return $this->entrust_status_text[$this->attributes['entrust_status']];
    }
    //司机确认是否装车
    public function getDstatusTextAttribute()
    {
        return $this->dstatus_text[$this->attributes['dstatus']];
    }
    //货主确认是否装车
    public function getOstatusTextAttribute()
    {
        return $this->ostatus_text[$this->attributes['ostatus']];
    }
    //货主支付状态
    public function getOwnerPayStatusTextAttribute()
    {
        return $this->owner_pay_status_text[$this->attributes['owner_pay_status']];
    }
    //司机支付状态
    public function getDriverPayStatusTextAttribute()
    {
        return $this->driver_pay_status_text[$this->attributes['driver_pay_status']];
    }
    //货主确认到达
    public function getOwnerArriveTextAttribute()
    {
        return $this->owner_arrive_text[$this->attributes['owner_arrive']];
    }
    //司机确认到达
    public function getDriverArriveTextAttribute()
    {
        return $this->driver_arrive_text[$this->attributes['driver_arrive']];
    }
    //是否上传回执
    public function getIsUploadTextAttribute()
    {
        return $this->is_upload_text[$this->attributes['is_upload']];
    }
    //卸货状态
    public function getIsUnloadTextAttribute()
    {
        return $this->is_unload_text[$this->attributes['is_unload']];
    }
    //货主是否评价
    public function getIsOwnerCommentTextAttribute()
    {
        return $this->is_owner_comment_text[$this->attributes['is_owner_comment']];
    }
    //车主是否评价
    public function getIsDriverCommentTextAttribute()
    {
        return $this->is_driver_comment_text[$this->attributes['is_driver_comment']];
    }

    public function comments()
    {
        return $this->hasOne(Comments::class, 'waybill_id', 'id');
    }

    public function owner()
    {
        return $this->hasOne(User::class, 'id', 'owner_id')->select('id', 'name', 'real_name', 'phone', 'head_portrait');
    }

    public function driver()
    {
        return $this->hasOne(User::class, 'id', 'driver_id')->select('id', 'name', 'real_name', 'phone', 'head_portrait');
    }

    public function getFriendlyTimeAttribute()
    {
        return Carbon::createFromTimestamp(strtotime($this->attributes['created_at']))->diffForHumans();
    }

//    public function getCreatedAtAttribute($date)
//    {
//        return Carbon::createFromTimestamp(strtotime($date))->diffForHumans();
//    }
}
